<?php
require_once __DIR__ . '/../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;

$config = [
    'host' => '127.0.0.1',
    'vhost' => '/',
    'port' => 5672,
    'login' => 'guest',
    'password' => 'guest',
];

//队列名称
$queue = 'simple';

try {
    //建立连接到RabbitMQ服务器
    $connection = new AMQPStreamConnection($config['host'], $config['port'], $config['login'], $config['password'], $config['vhost']);
    //建立通道
    $channel = $connection->channel();
    //声明一个队列
    $channel->queue_declare($queue, false, false, false, false);
    echo " [*] Waiting for messages. To exit press CTRL+C\n";
    $callback = function ($msg) { // 回调函数
        echo ' [x] Received ', $msg->body, "\n";
    };
    $channel->basic_consume($queue, '', true, true, false, false, $callback);
    while ($channel->is_consuming()) { // 循环获取消息
        $channel->wait();
    }
    $channel->close();
    $connection->close();
} catch (Exception $e) {
    die($e->getMessage());
}